import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:video_player/video_player.dart';
import 'package:video_player_win/video_player_win.dart';

import '../../../../pojo/ImPojo.dart';
import '../../../../util/FileUtil.dart';
import '../../../../util/MessageUtil.dart';
import '../../../bloc/MessageListCubit.dart';
import '../../../sub/VideoPlayerPage.dart';
import '../UiUtil.dart';
import 'MessagePane.dart';

class VideoMessagePane extends StatelessWidget {
  final WrapImMessage wrapMessage;

  const VideoMessagePane({required this.wrapMessage, super.key});

  @override
  Widget build(BuildContext context) {
    return _VideoMessagePane(message: wrapMessage.message);
  }
}

class VideoMessageRefPane extends _VideoMessagePane {
  final MessageRefParam param;

  const VideoMessageRefPane({required super.message, required this.param, super.key});
}



class _VideoMessagePane extends StatefulWidget {
  final ImMessage message;

  const _VideoMessagePane({required this.message, super.key});

  @override
  State<StatefulWidget> createState() => _VideoMessageState();
}

class _VideoMessageState extends State<_VideoMessagePane> {
  VideoPlayerController? _controller;
  WinVideoPlayerController? _winVideoPlayerController;

  @override
  void initState() {
    super.initState();

    if (widget.message.messageExt == null) {
      widget.message.initMessageExt();
    }

    var videoPath = MessageUtil.getFilePath2(widget.message);
    if (Platform.isWindows) {
      _winVideoPlayerController = WinVideoPlayerController.file(File(videoPath))
        ..initialize().then((_) {
          setState(() {});
        });
    } else {
      _controller = VideoPlayerController.file(File(videoPath))
        ..initialize().then((_) {
          setState(() {});
        });
    }

  }

  @override
  Widget build(BuildContext context) {
    return _buildBackgroundPane(context);
  }

  Widget _buildBackgroundPane(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 10, right: 10),
      child: _buildPane(),
    );
  }

  Widget _buildPane() {
    return (Platform.isWindows ? _winVideoPlayerController!.value.isInitialized : _controller!.value.isInitialized) ? AspectRatio(
            aspectRatio: Platform.isWindows ? _winVideoPlayerController!.value.aspectRatio : _controller!.value.aspectRatio,
            child: GestureDetector(
              onTap: ()=> {
                Navigator.of(context).push(MaterialPageRoute(builder: (context){
                  return VideoPlayerPage(videoPath: MessageUtil.getFilePath2(widget.message),);
                }))
              },
              child: Stack(
                alignment: Alignment.center,
                children: [
                  if (Platform.isWindows) WinVideoPlayer(_winVideoPlayerController!),
                  if (!Platform.isWindows) VideoPlayer(_controller!),
                  buildSvgView('assets/svg/play.svg', 40, 40, Colors.white)
                ],
              ),
            ),
          )
        : const SizedBox(height: 20, width: 80,);
  }

  @override
  void dispose() {
    super.dispose();
    _controller?.dispose();
    _winVideoPlayerController?.dispose();
  }
}
